home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 10.2 KB | 385 lines | [TEXT/MPS ] |
- /*
- File: GatewayConfig.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __BLJOCEUTILITIES__
- #include "BLJOCEUtilities.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef __GATEWAYCONFIG__
- #include "GatewayConfig.h"
- #endif
-
- #ifndef __ADASTUPLEDATABASE__
- #include "ADASTupleDatabase.h"
- #endif
-
- #ifndef __OCESTANDARDDIRECTORY__
- #include "OCEStandardDirectory.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- #ifndef __STDGETDIR__
- #include "StdGetDir.h"
- #endif
-
-
- static TADASTupleDatabase* gConfigurationDB = nil;
-
- // This returns the current configuration database, or nil if there is none.
- TADASTupleDatabase* GetConfigurationDB()
- {
- return gConfigurationDB;
- }
-
- typedef struct {
- unsigned long version;
- OSType signature;
-
- PackedRecordID packedAdministratorRecordID;
- AuthKey authenticationKey;
-
- PackedDSSpec configurationPackedRecordID;
- } ConfigPreferencesDataRecord, **ConfigPreferencesDataHandle;
-
- const long kConfigPreferencesFileVersion = 1;
- const OSType kConfigPreferencesSignature = 'BovJ';
-
- #pragma segment GatewayConfig
-
- Handle ReadFileIntoHandle (FSSpec fileSpec) {
-
- Handle dataH = nil;
- short refNum = 0;
-
- TRY
- FAILOSErr(FSpOpenDF(&fileSpec, fsWrPerm, &refNum));
-
- long fileSize;
- FAILOSErr( GetEOF (refNum, &fileSize));
-
- dataH = FAILNewHandle(fileSize);
- FAILNULL(dataH);
-
- HLock((Handle) dataH);
- FAILOSErr(FSRead(refNum, &fileSize, (Ptr) *dataH));
-
- FAILOSErr(FSClose(refNum));
-
- EXCEPTION
- if (dataH)
- DeallocateHandle(dataH);
- dataH = nil;
-
- if (refNum)
- FSClose(refNum);
-
- ENDEXCEPTION;
-
- return dataH;
- };
-
-
-
- void DisplayConfigDB();
-
- // This function 'sets up' the configuration database. The whichDB parameters should be
- // the name of a file of type 'pref', creator 'BOVJ', located in the boot volume's
- // preferences folder. This file contains the information which is needed to
- // actually bootstrap the configuration database. If the configuration database
- // can correctly be set up, then this function returns true, otherwise it returns false.
-
- Boolean SetupConfigurationDB (short vRefNum, long dirID, Str63 whichDB) {
- ConfigPreferencesDataHandle dataH = nil;
-
- #if debug
- if (gConfigurationDB != nil) {
- keith << "SetupConfigurationDB called, but a configDB already exists." << endl << flush;
- };
- #endif
- TRY
- FSSpec fileSpec;
- FAILOSErr(FSMakeFSSpec(vRefNum, dirID, whichDB, &fileSpec));
-
- dataH = (ConfigPreferencesDataHandle) ReadFileIntoHandle (fileSpec);
- FAILNULL(dataH);
-
- FAILifFALSE(OCEValidPackedRecordID(&(**dataH).packedAdministratorRecordID));
-
- RecordID userRecord;
- OCEUnpackRecordID (&(**dataH).packedAdministratorRecordID, &userRecord);
-
- AuthKey key = (**dataH).authenticationKey;
-
- // Create an identity, using the recordID and key from the preferences file.
- AuthIdentity identity = 0;
- { AuthBindIdentityPB bindIdentityPB;
-
- memset(&bindIdentityPB, 0, sizeof(bindIdentityPB));
- bindIdentityPB.dsRefNum = 0;
- bindIdentityPB.userIdentity = 0;
- bindIdentityPB.userRecord = &userRecord;
- bindIdentityPB.userKey = &key;
-
- keith << "CreateID: user=" << userRecord << " key="; keith.write((char*) &key, (int) sizeof(key)); keith << endl << flush;
-
- OSErr err = AuthBindIdentity((AuthParamBlockPtr) &bindIdentityPB, false);
-
- keith << "SetupConfigurationDB, creating identity with ASCreateIdentity(), id:" << bindIdentityPB.userIdentity << " err=" << err << endl;
-
- if ((err != noErr) && (err != daIdentityExists))
- FAILOSErr(err);
-
- identity = bindIdentityPB.userIdentity;
- };
-
- FAILifFALSE(OCEValidPackedDSSpec(&(**dataH).configurationPackedRecordID));
-
- DSSpec spec;
- RecordID configurationRecord;
- OCEUnpackDSSpec(&(**dataH).configurationPackedRecordID, &spec, &configurationRecord);
-
- // Now, create the configuration database. Finally.
- gConfigurationDB = new TADASTupleDatabase();
- FAILNULL(gConfigurationDB);
-
- FAILifFALSE( ((TADASTupleDatabase*) gConfigurationDB)->IADASTupleDatabase(identity, 0, &configurationRecord, nil));
-
- DeallocateHandle((Handle) dataH);
-
- // DisplayConfigDB();
-
- EXCEPTION
- DeallocateHandle((Handle) dataH);
- delete gConfigurationDB;
- gConfigurationDB = nil;
-
- ENDEXCEPTION;
-
- return (gConfigurationDB != nil);
- };
-
- OSErr WriteHandleToFile (FSSpec fileSpec, Handle dataH) {
- OSErr err = noErr;
- short refNum = 0;
-
- TRY
- err = FSpCreate (&fileSpec, 'pref', 'BOVJ', smRoman);
- if ((err != noErr) && (err != dupFNErr))
- FAILOSErr(err);
-
- FAILOSErr(FSpOpenDF(&fileSpec, fsWrPerm, &refNum));
-
- HLock((Handle) dataH);
- long dataSize = GetHandleSize((Handle) dataH);
- FAILOSErr(FSWrite(refNum, &dataSize, (Ptr) *dataH));
-
- FAILOSErr(FSClose(refNum));
-
- EXCEPTION
- if (refNum)
- FSClose(refNum);
- err = EXCEPTIONCODE;
-
- ENDEXCEPTION;
-
- return noErr;
- };
-
-
- // This function creates a configuration file in the local preferences file (see the
- // SetupConfigurationDB function). This function does not set up the configuration
- // database. If the preferences file is successfully created, then this function
- // returns true, otherwise it returns false.
-
- Boolean CreateConfigurationDB(short vRefNum, long dirID, Str63 whichDB) {
- Boolean result = false;
- ConfigPreferencesDataHandle dataH = nil;
- short refNum = 0;
- RecordID recordID;
-
- recordID.rli = nil;
- recordID.local.recordName = nil;
- recordID.local.recordType = nil;
-
- TRY
- // Now, create the data that gets written to the preferences file.
- dataH = (ConfigPreferencesDataHandle) FAILNewHandle( sizeof(**dataH));
- (**dataH).version = kConfigPreferencesFileVersion;
- (**dataH).signature = kConfigPreferencesSignature;
-
- RString password;
- AuthIdentity identity;
- DirectoryName directoryName;
- DirDiscriminator dirDiscriminator;
-
- FAILOSErr(SDPPromptForIdentityAndPassword(&identity, &password, nil, "\pLog-on as the Gateway Administrator…", false,
- &directoryName, &dirDiscriminator));
-
- { // Allocate the fields in the recordID
- memset (&recordID, 0, sizeof(recordID));
- recordID.rli = (PackedRLIPtr) FAILNewPtr(kRLIMaxBytes);
- recordID.local.recordName = (RStringPtr) FAILNewPtr(sizeof(RString));
- recordID.local.recordName->dataLength = kRStringMaxBytes;
- recordID.local.recordType = (RStringPtr) FAILNewPtr(sizeof(RString));
- recordID.local.recordType->dataLength = kRStringMaxBytes;
- };
-
-
- { AuthGetIdentityInfoPB getIdentityInfoPB;
-
- memset(&getIdentityInfoPB, 0, sizeof(getIdentityInfoPB));
- getIdentityInfoPB.userIdentity = identity;
- getIdentityInfoPB.userRecord = &recordID;
- FAILOSErr(AuthGetIdentityInfo((AuthParamBlockPtr) &getIdentityInfoPB, false));
- };
-
- OCEPackRecordID (&recordID, &(**dataH).packedAdministratorRecordID, sizeof((**dataH).packedAdministratorRecordID));
-
- keith << "ASPromptForRID: RecordID = " << recordID << " pwd=" << password << endl;
-
- { AuthPasswordToKeyPB passwordToKeyPB;
- AuthKey key;
-
- memset(&passwordToKeyPB, 0, sizeof(passwordToKeyPB));
- passwordToKeyPB.key = &key;
- passwordToKeyPB.password = &password;
-
- FAILOSErr( AuthPasswordToKey((AuthParamBlockPtr) &passwordToKeyPB, false));
- keith << "ASPwdToKey: key='"; keith.write((char *) &key, sizeof(key)); keith << "'" << endl;
- (**dataH).authenticationKey = key;
- };
-
-
- // Now, have the user find the configuration record for this gateway, using the CollabPack SDGetDirectories window
- PackedDSSpecPtr recordFound = nil;
- TRY
- RString type;
- RStringPtr typesArray[1];
-
- OCECToRString("ApplelinkGwyCnfg", smRoman, &type, kRStringMaxBytes);
- typesArray[0] = &type;
-
- FAILOSErr(SDPGetDirectories(&recordFound, nil, typesArray, 1, "\pSelect the gateway configuration…", identity));
-
- EXCEPTION
- DeallocatePtr((Ptr) recordFound);
-
- ENDEXCEPTION;
-
- OCECopyPackedDSSpec(recordFound, &(**dataH).configurationPackedRecordID, sizeof((**dataH).configurationPackedRecordID));
-
- // Now, make a file in the given folder, and put this goofy data into it.
- FSSpec fileSpec;
- { OSErr err = FSMakeFSSpec(vRefNum, dirID, whichDB, &fileSpec);
- if ((err != noErr) && (err != fnfErr))
- FAILOSErr(err);
- };
- FAILOSErr(WriteHandleToFile(fileSpec, (Handle) dataH));
-
- // We're done!
-
- DeallocatePtr((Ptr) recordID.rli);
- DeallocatePtr((Ptr) recordID.local.recordName);
- DeallocatePtr((Ptr) recordID.local.recordType);
-
- DeallocateHandle((Handle) dataH);
-
- result = true;
-
- EXCEPTION
- DeallocatePtr((Ptr) recordID.rli);
- DeallocatePtr((Ptr) recordID.local.recordName);
- DeallocatePtr((Ptr) recordID.local.recordType);
-
- DeallocateHandle( (Handle) dataH);
-
- result = false;
-
- ENDEXCEPTION;
-
- return result;
- };
-
-
-
- #if debug
- void DisplayConfigDB() {
- TADASTupleDatabase* db = GetConfigurationDB();
-
- if (db) {
- long count = db->CountTuples();
-
- for (long index = 1; index <= count; ++index) {
- long keySize = 64, dataSize = 768, actualDataSize;
- char key[64], data[768];
-
- if (db->GetIndexTuple(index, (void*) key, keySize, (void*) data, dataSize)) {
- keith << " #" << index << " key="; keith.write(key, (int) keySize);
- keith << " data=(" << dataSize << ") "; keith.write(data, (int) shortmin(32, dataSize)); keith << endl << flush;
- } else {
- keith << " #" << index << " could not be loaded, keySize=" << keySize << endl;
- };
- };
- } else {
- keith << "DisplayConfigDB: no configDB exists." << endl << flush;
- };
- };
- #endif
-
-
-
- #if debug
-
- // This function creates a 'special' preferences file which will use a local (non-ADAS)
- // database instead of the ADAS database setup by the standard CreateConfigurationDB().
-
- Boolean CreateLocalConfigurationDB(Str63 whichDB);
-
- #endif
-
-
- // These are a bunch of 'short-hand' functions for getting certain types of data out
- // of the configuration database.
-
- Boolean GetConfigRString (char* itemName, RString& item);
- Boolean GetConfigLong (char* itemName, long& item);
-
-
-
- Boolean GetConfigCString (char* itemName, char* buffer, short maxBufferSize) {
- TADASTupleDatabase* db = GetConfigurationDB();
- if (db) {
- long dataLength;
- if (db->GetTupleSize((void*) itemName, strlen(itemName), dataLength))
- if (dataLength+1 < maxBufferSize) {
- long actualDataSize = 0;
- if (db->GetTuple((void*) itemName, strlen(itemName), actualDataSize, (void*) buffer, maxBufferSize)) {
- buffer[dataLength] = 0;
- return true;
- };
- };
- };
- return false;
- };
-